package vn.edu.hcmus.fit.se.mdd.mbuid.adaptedaui.diagram.edit.parts;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import vn.edu.hcmus.fit.se.mdd.mbuid.adaptedaui.diagram.part.AdaptedauiVisualIDRegistry;

/**
 * @generated
 */
public class AdaptedauiEditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (AdaptedauiVisualIDRegistry.getVisualID(view)) {

			case PresentationSystemEditPart.VISUAL_ID:
				return new PresentationSystemEditPart(view);

			case PresentationUnitEditPart.VISUAL_ID:
				return new PresentationUnitEditPart(view);

			case PresentationUnitNameEditPart.VISUAL_ID:
				return new PresentationUnitNameEditPart(view);

			case AbstractPresentationEditPart.VISUAL_ID:
				return new AbstractPresentationEditPart(view);

			case AbstractPresentationNameEditPart.VISUAL_ID:
				return new AbstractPresentationNameEditPart(view);

			case AbstractUIComponentEditPart.VISUAL_ID:
				return new AbstractUIComponentEditPart(view);

			case AbstractUIComponentNameEditPart.VISUAL_ID:
				return new AbstractUIComponentNameEditPart(view);

			case MultipleChoiceEditPart.VISUAL_ID:
				return new MultipleChoiceEditPart(view);

			case MultipleChoiceNameEditPart.VISUAL_ID:
				return new MultipleChoiceNameEditPart(view);

			case InputFieldEditPart.VISUAL_ID:
				return new InputFieldEditPart(view);

			case InputFieldNameEditPart.VISUAL_ID:
				return new InputFieldNameEditPart(view);

			case OutputFieldEditPart.VISUAL_ID:
				return new OutputFieldEditPart(view);

			case OutputFieldNameEditPart.VISUAL_ID:
				return new OutputFieldNameEditPart(view);

			case NavigatorEditPart.VISUAL_ID:
				return new NavigatorEditPart(view);

			case NavigatorNameEditPart.VISUAL_ID:
				return new NavigatorNameEditPart(view);

			case SingleChoiceEditPart.VISUAL_ID:
				return new SingleChoiceEditPart(view);

			case SingleChoiceNameEditPart.VISUAL_ID:
				return new SingleChoiceNameEditPart(view);

			case ActivatorEditPart.VISUAL_ID:
				return new ActivatorEditPart(view);

			case ActivatorNameEditPart.VISUAL_ID:
				return new ActivatorNameEditPart(view);

			case AbstractUIComponent2EditPart.VISUAL_ID:
				return new AbstractUIComponent2EditPart(view);

			case AbstractUIComponentName2EditPart.VISUAL_ID:
				return new AbstractUIComponentName2EditPart(view);

			case PresentationUnitUIComponentCompartmentEditPart.VISUAL_ID:
				return new PresentationUnitUIComponentCompartmentEditPart(view);

			case AbstractPresentationUIComponentCompartmentEditPart.VISUAL_ID:
				return new AbstractPresentationUIComponentCompartmentEditPart(
						view);

			case UITemporalRelationshipEditPart.VISUAL_ID:
				return new UITemporalRelationshipEditPart(view);

			case UITemporalRelationshipLabelEditPart.VISUAL_ID:
				return new UITemporalRelationshipLabelEditPart(view);

			case UITemporalRelationship2EditPart.VISUAL_ID:
				return new UITemporalRelationship2EditPart(view);

			case UITemporalRelationshipLabel2EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel2EditPart(view);

			case UITemporalRelationship3EditPart.VISUAL_ID:
				return new UITemporalRelationship3EditPart(view);

			case UITemporalRelationshipLabel3EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel3EditPart(view);

			case UITemporalRelationship4EditPart.VISUAL_ID:
				return new UITemporalRelationship4EditPart(view);

			case UITemporalRelationshipLabel4EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel4EditPart(view);

			case UITemporalRelationship5EditPart.VISUAL_ID:
				return new UITemporalRelationship5EditPart(view);

			case UITemporalRelationshipLabel5EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel5EditPart(view);

			case UITemporalRelationship6EditPart.VISUAL_ID:
				return new UITemporalRelationship6EditPart(view);

			case UITemporalRelationshipLabel6EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel6EditPart(view);

			case UITemporalRelationship7EditPart.VISUAL_ID:
				return new UITemporalRelationship7EditPart(view);

			case UITemporalRelationshipLabel7EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel7EditPart(view);

			case UITemporalRelationship8EditPart.VISUAL_ID:
				return new UITemporalRelationship8EditPart(view);

			case UITemporalRelationshipLabel8EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel8EditPart(view);

			case UITemporalRelationship9EditPart.VISUAL_ID:
				return new UITemporalRelationship9EditPart(view);

			case UITemporalRelationshipLabel9EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel9EditPart(view);

			case UITemporalRelationship10EditPart.VISUAL_ID:
				return new UITemporalRelationship10EditPart(view);

			case UITemporalRelationshipLabel10EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel10EditPart(view);

			case UITemporalRelationship11EditPart.VISUAL_ID:
				return new UITemporalRelationship11EditPart(view);

			case UITemporalRelationshipLabel11EditPart.VISUAL_ID:
				return new UITemporalRelationshipLabel11EditPart(view);

			case UIContainerRelationshipEditPart.VISUAL_ID:
				return new UIContainerRelationshipEditPart(view);

			case PresentationContainerRelationshipEditPart.VISUAL_ID:
				return new PresentationContainerRelationshipEditPart(view);

			case CommandDestinationsEditPart.VISUAL_ID:
				return new CommandDestinationsEditPart(view);

			case PresentationTemporalRelationshipEditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipEditPart(view);

			case PresentationTemporalRelationshipLabelEditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabelEditPart(view);

			case PresentationTemporalRelationship2EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship2EditPart(view);

			case PresentationTemporalRelationshipLabel2EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel2EditPart(view);

			case PresentationTemporalRelationship3EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship3EditPart(view);

			case PresentationTemporalRelationshipLabel3EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel3EditPart(view);

			case PresentationTemporalRelationship4EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship4EditPart(view);

			case PresentationTemporalRelationshipLabel4EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel4EditPart(view);

			case PresentationTemporalRelationship5EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship5EditPart(view);

			case PresentationTemporalRelationshipLabel5EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel5EditPart(view);

			case PresentationTemporalRelationship6EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship6EditPart(view);

			case PresentationTemporalRelationshipLabel6EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel6EditPart(view);

			case PresentationTemporalRelationship7EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship7EditPart(view);

			case PresentationTemporalRelationshipLabel7EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel7EditPart(view);

			case PresentationTemporalRelationship8EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship8EditPart(view);

			case PresentationTemporalRelationshipLabel8EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel8EditPart(view);

			case PresentationTemporalRelationship9EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship9EditPart(view);

			case PresentationTemporalRelationshipLabel9EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel9EditPart(view);

			case PresentationTemporalRelationship10EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship10EditPart(view);

			case PresentationTemporalRelationshipLabel10EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel10EditPart(view);

			case PresentationTemporalRelationship11EditPart.VISUAL_ID:
				return new PresentationTemporalRelationship11EditPart(view);

			case PresentationTemporalRelationshipLabel11EditPart.VISUAL_ID:
				return new PresentationTemporalRelationshipLabel11EditPart(view);

			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				if (getWrapLabel().isTextWrapOn()
						&& getWrapLabel().getText().length() > 0) {
					rect.setSize(new Dimension(text.computeSize(rect.width,
							SWT.DEFAULT)));
				} else {
					int avr = FigureUtilities.getFontMetrics(text.getFont())
							.getAverageCharWidth();
					rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
							SWT.DEFAULT)).expand(avr * 2, 0));
				}
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
